﻿@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Newsletters
@model NewsletterListViewModel
@{
	ViewBag.Title = "Newsletter Issues";
	Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

@using (Html.BeginForm("Index", "Newsletters", FormMethod.Get, new { @class = "form-horizontal" }))
{
	<fieldset class="border1">
		<legend class="border1">Search Issues</legend>
		<div class="row">
            @Html.HiddenFor(m => m.SortOrder)
			<div class="col-sm-3">
				<div class="form-group">
					@Html.LabelFor(model => model.StartDate, new { @class = "col-sm-6 control-label" })
					<div class="col-sm-6">
						@Html.TextBoxFor(m => m.StartDate, "{0:" + Constants.DATE_FORMAT + "}", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.StartDate)
					</div>
				</div>
			</div>
			<div class="col-sm-3">
				<div class="form-group">
					@Html.LabelFor(model => model.EndDate, new { @class = "col-sm-6 control-label" })
					<div class="col-sm-6">
						@Html.TextBoxFor(m => m.EndDate, "{0:" + Constants.DATE_FORMAT + "}", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.EndDate)
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="form-group">
					@Html.LabelFor(model => model.SelectedIssueType, new { @class = "col-sm-4 control-label" })
					<div class="col-sm-8">
						@Html.DropDownListFor(m => m.SelectedIssueType, Model.IssueTypes, new { @class = "form-control" })
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-sm-6">
				<div class="form-group">
					@Html.LabelFor(model => model.Symbols, new { @class = "col-sm-3 control-label" })
					<div class="col-sm-9">
						@Html.TextBoxFor(m => m.Symbols, new { @class = "form-control" })
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="form-group">
					@Html.LabelFor(model => model.SelectedProduct, new { @class = "col-sm-4 control-label" })
					<div class="col-sm-8">
						@Html.DropDownListFor(m => m.SelectedProduct, Model.ProductTypes, new { @class = "form-control" })
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-sm-6">
				<div class="form-group">
					@Html.LabelFor(model => model.Title, new { @class = "col-sm-3 control-label" })
					<div class="col-sm-9">
						@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })
					</div>
				</div>
			</div>
			<div class="col-sm-6">
				<div class="form-group">
                    @Html.LabelFor(model => model.MinAccessLevel, new { @class = "col-sm-4 control-label" })
			        <div class="col-sm-8">
				        @Html.DropDownListFor(m => m.MinAccessLevel, Model.AccessLevels, new { @class = "form-control" })
			        </div>
				</div>
			</div>
		</div>
        <div class="row">
            <div class="col-sm-6">
            </div>
            <div class="col-sm-6">
				<div class="pull-right">
					<button type="submit" id="find-issues" class="btn btn-default">Find Issues</button>
				</div>
			</div>
        </div>
	</fieldset>
}
<div class="table_container">
	<div id="newsletters-list">
		@Html.Partial("NewslettersList")
	</div>
	<button class="btn btn-default" data-toggle="modal" data-target="#add-dialog">Upload New</button>
</div>

<!-- Modal -->
<div class="modal fade" id="add-dialog" tabindex="-1" role="dialog" aria-labelledby="add-dialog-label" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
		        @using (Html.BeginForm("Add", "Newsletters", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="add-dialog-label">Upload Newsletter</h4>
      </div>
      <div class="modal-body">

	@Html.ValidationSummary(true)

		@Html.Partial("EditorTemplates/Newsletter", Model.EditModel)

      </div>
      <div class="modal-footer">
		  <div class="row">
		<div class="col-sm-12">
			<div class="pull-right">
				<button type="submit" class="btn btn-default width80">Upload</button>
				<a href="@Url.Action("Index")" class="btn btn-default width80 cancel" data-dismiss="modal">Cancel</a>
			</div>
		</div>
	</div>
      </div>
		  }
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="success-dialog" tabindex="-1" role="dialog" aria-labelledby="add-dialog-label" aria-hidden="true">
    <div class="modal-dialog">
	    <div class="modal-content">
            <div class="modal-header">
			    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="success-dialog-label">Message</h4>
			</div>
			<div class="modal-body">
			    Message Sent
			</div>
			<div class="modal-footer">
			    <div class="row">
				    <div class="col-sm-12">
					    <div class="pull-right">
						    <a href="#" class="btn btn-default width80" data-dismiss="modal">OK</a>
						</div>
					</div>
				</div>

			</div>
		<!-- /.modal-content -->
		</div>
    <!-- /.modal-dialog -->
	</div>
</div>

<script type="text/javascript">
	$(function () {

	    RemoveTime("#@Html.IdFor(m=>m.StartDate)");
	    RemoveTime("#@Html.IdFor(m=>m.EndDate)");
	    $("#@Html.IdFor(m => m.StartDate), #@Html.IdFor(m => m.EndDate)").datepicker();

		$("#find-issues").click(function (e) {
			e.preventDefault();
			GetList();
		});

		$("#add-dialog .cancel").click(function () {
			$("#add-dialog input").val('');
			$("#add-dialog select").val('');
			$("#add-dialog input[id$='Date']").datepicker("setDate", new Date());
		});
	});

    function GetList() {
        $.ajax({
            url: "@Url.Action("Index")",
            cache: false,
            data: {
                startDate: $("#@Html.IdFor(m => m.StartDate)").val(),
				    endDate: $("#@Html.IdFor(m => m.EndDate)").val(),
				    selectedProduct: $("#@Html.IdFor(m => m.SelectedProduct)").val(),
				    issueType: $("#@Html.IdFor(m => m.SelectedIssueType)").val(),
				    symbols: $("#@Html.IdFor(m => m.Symbols)").val(),
                    title: $("#@Html.IdFor(m => m.Title)").val(),
                    minAccessLevel: $("#@Html.IdFor(m => m.MinAccessLevel)").val(),
                    sort: $("#@Html.IdFor(m => m.SortOrder)").val(),
                    page: '@(Model.Newsletters.PageNumber.ToString())'
				},
            success: function (result) {
                $("#newsletters-list").html(result);
            }
        });
    }

    function Delete(id) {
        $.ajax({
            url: "@Url.Action("Delete")",
            cache: false,
            data: {
                id: id
            },
            success: function (result) {
                GetList();
            }
        });
    }

    function Hide(id) {
        $.ajax({
            url: "@Url.Action("Hide")",
            cache: false,
            data: {
                id: id
            },
            success: function (result) {
                GetList();
            }
        });
    }

    function Show(id) {
        $.ajax({
            url: "@Url.Action("Show")",
            cache: false,
            data: {
                id: id
            },
            success: function (result) {
                GetList();
            }
        });
    }

    function Send(id) {
        @if(Model.IsSendLinkVisible)
        {
            <text>
            $.ajax({
                url: "@Url.Action("Send")",
                cache: false,
                data: {
                    id: id
                },
                success: function (result) {
                    $("#success-dialog").modal();
                }
            });
            </text>
        }
        else
        {
            <text>
            alert("Only US users can send newsletters");
            </text>
        }
    }

    function SendTester(id) {
        $.ajax({
            url: "@Url.Action("SendTester")",
            cache: false,
            data: {
                id: id
			    },
            success: function (result) {
                $("#success-dialog").modal();
            }
        });
    }

</script>

